package frameworks.widgets
{
	import flash.utils.Dictionary;
	
	public class SenderManager
	{
		private var _oid:uint = 0;
		private var _watcherFunctions:Dictionary = new Dictionary(true);//使用弱引用，删除watcher时不必移除
		private var _initWatcherFunction:Dictionary = new Dictionary(true);//使用弱引用，删除watcher时不必移除
		private var _changedSenders:Dictionary = new Dictionary(true);
		private var _changed:Boolean;
		private var _senderHasOrder:Boolean;
		
		public function SenderManager()
		{
		}
		
		public function updateChanges(args:Object=null):void{
			var changedSenders:Dictionary;
			var updateFuns:Dictionary;
			var arr:Array;
			var sender:*;
			var changedSendersList:Array;
			var i:int;
			var item:*;
			var fun:*;
			while(_changed){//每轮更新后重复查看上轮更新后，有没有产生新的改动
				trace("SenderManager=====updateChanges");
				_changed = false;
				changedSenders = _changedSenders;//暂存_changedSenders
				_changedSenders = new Dictionary(true);//在此轮之后产生的change将归于下轮更新
				changedSendersList = [];
				if(_senderHasOrder){//如果Sender有序
					arr = [];
					for(sender in changedSenders){
						arr.push({order:BaseSender(sender).levelID, value:sender});
					}
					arr.sortOn("order",Array.NUMERIC);//将Sender 排序
					for each(item in arr){
						changedSendersList.push(item.value);
					}
				}else{
					for(sender in changedSenders){
						changedSendersList.push(sender);
					}
				}
				arr = [];
				/*i = changedSendersList.length;
				while(i > 0){
				i--;
				ILayer(affectedList[i]).preSubmit();
				}*/
				for each(sender in changedSendersList){
					if(_changedSenders[sender] !== undefined ){//在真正update之前，可以剔除重复更新的sender
						trace("delete repeat sender",sender);
						delete _changedSenders[sender];
					}
					BaseSender(sender).preVerify();
				}
				for each(sender in changedSendersList){
					BaseSender(sender).frontVerify();
				}
				i = changedSendersList.length;
				while(i > 0){
					i--;
					BaseSender(changedSendersList[i]).backVerify();
				}
				updateFuns = _initWatcherFunction;//创建此轮需要更新Wather Functions列表；
				_initWatcherFunction = new Dictionary(true);
				for each(sender in changedSendersList){
					BaseSender(sender).sumbitChangedFuns(updateFuns);//循环各Sender，将各自需要通知的Wather Functions提交到changedFuns列表中
				}
				arr = [];
				for(fun in updateFuns){
					arr.push({order:_watcherFunctions[fun],fun:fun,value:updateFuns[fun]});
				}
				arr.sortOn("order",Array.NUMERIC);//将Wather Functions 排序
				for each(item in arr){item.fun(item.value);}//通知各Wather Functions。item.value为true表示为初始第一笔绑定
				for each(sender in changedSendersList){
					BaseSender(sender).senderAllComplete();//循环各Sender，执行提交后的清空
				}
			}
		}
		internal function senderChanged(sender:BaseSender):void{
			_changedSenders[sender] = true;
			if(!_changed){_changed = true;}
		}
		internal function addInitWatcherFunction(fun:Function):void{
			_initWatcherFunction[fun] = true;
			if(!_changed){_changed = true;}
		}
		internal function regWatcherFunction(fun:Function):void{
			_oid ++;
			if(_oid > 1000000){
				var tem:Array = [];
				for(var f:* in _watcherFunctions){
					tem.push({order:_watcherFunctions[f], fun:f});
				}
				tem.sortOn("order",Array.NUMERIC);
				for (var i:uint=0,k:uint=tem.length;i<k;i++){
					_watcherFunctions[tem[i].fun] = i;
				}
				i ++;
				_oid = i;
			}
			_watcherFunctions[fun] = _oid;
		}
	}
}